home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / TMCM Software and Labs.sit / Software for TMCM 7_95 / Files for Lab 6 / PrimeNumbers < prev   
Text File  |  1995-07-10  |  6KB  |  149 lines

  1. ; This file contains a subroutine, "remainder", for computing
  2. ; remainders.  The subroutine is at the end of the file.  You'll
  3. ; find a description of the subroutine there.
  4.  
  5. ; Your assignment is to write a main program that will generate
  6. ; a list of prime numbers.  You can just follow the algorithm
  7. ; that is outlined in the comments.  All the labeled data 
  8. ; locations that you will need are already declared,
  9. ; and some of the program is already written.  Read the
  10. ; comments carefully.  You only have to add about 16 instructions
  11. ; to the program, and 7 of those are just for calling the
  12. ; subroutine.
  13.  
  14. ; Once the program is written, you can let the program run at
  15. ; full speed and watch the primes being added to the list
  16. ; starting at location 50.  It is also interesting to watch
  17. ; the program at full speed in Graphics Display mode.
  18.  
  19. ; -------------------- MAIN PROGRAM ---------------------------------
  20.  
  21.              LOD-C  2    ; 2 is the first number that will be tested.
  22.              STO    p    ; Memory location "p" holds the number that
  23.                          ;    is currently begin tested.
  24.  
  25.              LOD-C  0             ; Make sure there is a zero to mark
  26.              STO    prime_list    ; the end of the list of primes.
  27.  
  28.              LOD-C  prime_list    ; "end_of_list" points to the first
  29.              STO    end_of_list   ; available location in the list of primes.
  30.  
  31. loop:                    ; The main loop of the program starts here.
  32.                          ; In this loop, the number "p" is tested for
  33.                          ; primality.  If it is prime, p is added to
  34.                          ; the list of prime numbers; if not, it is
  35.                          ; simply discarded.
  36.  
  37.                          ; First, set "ptr" to point to the start
  38.                          ; of the list of primes.
  39.  
  40. get_d:                   ; Get the number, d, in the address indicated
  41.                          ; by ptr.  (Use indirect addressing!)
  42.  
  43.                          ; If the number d is zero, then p is prime, so
  44.                          ; jump to location "add_to_list" (to add p
  45.                          ; to the list of primes and then go on to the
  46.                          ; next value of p).
  47.  
  48.                          ; In the case d is not zero, check whether
  49.                          ; the remainder is zero when p is divided by d,
  50.                          ; using the subroutine.  If so, then p is NOT
  51.                          ; prime.  Go on to the next value of p.
  52.  
  53.                          ; In the case where the remainder is NOT zero,
  54.                          ; add 1 to ptr and jump back to "get_d" to
  55.                          ; test the next number in the list.
  56.  
  57.                          ; (The rest of the program is already written.)
  58.  
  59. add_to_list:             ; Add p to the list of prime numbers
  60.              LOD p
  61.              STO-I end_of_list
  62.              LOD   end_of_list
  63.              INC
  64.              STO   end_of_list
  65.              LOD-C 0            ; (Make sure there is a zero at the end of the list.)
  66.              STO-I end_of_list 
  67.                          
  68. next_p:      LOD    p    ; Add 1 to p (the number being tested)
  69.              INC
  70.              STO    p
  71.              JMP    loop ; and go back to the start of the main loop.
  72.                          ; (This is an infinite loop that will never end!)
  73.  
  74.                          ; Finally, here are the data locations needed
  75.                          ; in the maim program.  (The main program
  76.                          ; will also need to refer to the location
  77.                          ; "prime_list", which is the starting location
  78.                          ; for the list of primes.  And when it calles
  79.                          ; the subroutine, it will need to use the
  80.                          ; memory locations "ret_adr", "n", "d",
  81.                          ; and possibly "rem".)
  82.  
  83. p:           data        ; The number begin tested for primality.
  84.  
  85. ptr:         data        ; A pointer to one of the primes already
  86.                          ; in the list of primes.  (That is, the number
  87.                          ; stored in ptr is one of the numbers in the list.)
  88.  
  89. end_of_list: data        ; A pointer to the location that follows the
  90.                          ; end of the list of primes.  When a new prime
  91.                          ; is found, it is placed in this location.  (You
  92.                          ; won't need to worry about this in the part of
  93.                          ; the program you are writing.)
  94.  
  95. @50  ; The list of prime numbers will start at location 50
  96.  
  97. prime_list:  data 
  98.  
  99.  
  100. ; --------------------------  Subroutine "remainder" ----------------
  101.  
  102. ; This subroutine computes the remainder when one postive number, n,
  103. ; is divided by another positibe number, d.  To use it:
  104. ;     (1) store the number n in memory location "n"
  105. ;     (2) store the number d in memory location "d"
  106. ;     (3) store the return address in location "ret_adr"
  107. ;     (4) jump to location "remainder"
  108. ; When the subroutine terminates, the remainder will be in memory
  109. ; location "rem."  It will ALSO be in the accumulator (AC).
  110.  
  111. @900  ; subroutine loads starting at location 900
  112.  
  113. ret_adr:   data    ; return address
  114.  
  115. n:         data    ; parameters, as described above
  116. d:         data
  117. rem:       data
  118.  
  119. remainder: LOD   d       ; Starting point for subroutine.
  120.            STO   temp_r  ; (You don't need to understand anything
  121.                          ;               from here on.)
  122.  
  123. L1r:       LOD   temp_r
  124.            SHL
  125.            STO   temp_r
  126.            SUB   n
  127.            JMN   L1r
  128.            JMZ   L1r
  129.  
  130. L2r:       LOD   temp_r
  131.            SHR
  132.            STO   temp_r
  133.            SUB   d
  134.            JMN   done_r
  135.            LOD   n
  136.            SUB   temp_r
  137.            JMN   L2r
  138.            STO   n
  139.            JMP   L2r
  140. done_r:    LOD   n
  141.            STO   rem
  142.  
  143.            JMP-I  ret_adr
  144.  
  145. temp_r:    data
  146.  
  147.  
  148.  
  149.